<?php
namespace app\index\controller\user;
use app\index\model\UsersAddress;
class AddressAdd extends \app\index\controller\user\BaseUser{
    public function index(){
        //地址修改
        $request = \think\Request::instance();
        $address_id = $request->get('id');
        //判断是编辑模式还是非编辑模式
        if($address_id){
            $users_address = new UsersAddress();
            //查询该会员信息
            $users_address->where('users_id',$this->userdata['id']);
            //查询传过来的id的数据
            $users_address->where('id',$address_id);
            $item = $users_address->find();
            if(!$item){
                $this->error('无效的信息！请重新操作！','index/user.AddressControl/index');
            }
            //更改页面title
            $item->page_title = '修改地址';            
        }else{
            //地址添加
            //添加地址的情况下设置默认值 stdClass是空对象 "\"代表没有命名空间
            $item = new \stdClass;
            $item->page_title = '添加地址';
            $item->recipient = '';
            $item->phone = '';
            $item->city = '';
            $item->street = '';
            $item->detailed = '';
        }

        $data['item'] = $item;
        return view('user/addressAdd',$data);
    }
    public function save(){
        $request = \think\Request::instance();

        $data['recipient'] = $request->post('recipient');
        $data['phone'] = $request->post('phone');
        $data['city'] = $request->post('city');
        $data['street'] = $request->post('street');
        $data['detailed'] = $request->post('detailed');

        $validate = new \think\Validate([
            'recipient|收件人'  => 'require',
            'phone|收件人手机号码' => 'require|\d+',
            'city|收件人城市' => 'require',
            'street|收件人街道地址' => 'require',
            'detailed|详细地址' => 'require',
        ]);

        if (!$validate->check($data)) {
            return $this->error($validate->getError());
        }
        //将数据插入数据库  
        $users_address = new UsersAddress();
        //会员id
        $data['users_id'] = $this->userdata['id'];
        $is = $users_address->allowField(true)->save($data);      
        if($is){
            return $this->success('地址添加成功！','index/user.AddressControl/index');
        }else{
            return $this->error('地址添加失败！');
        }
    }
    public function addressDel(){
        $request = \think\Request::instance();
        $address_id = $request->get('id');
        //判断是不是该用户的地址信息
        if($address_id){
            $users_address = new UsersAddress();
            //查询该会员信息
            $users_address->where('users_id',$this->userdata['id']);
            //查询传过来的id的数据
            $users_address->where('id',$address_id);
            $item = $users_address->find();
            if(!$item){
                $this->error('无效的信息！请重新操作！','index/user.AddressControl/index');
            }
            //逻辑删除该条数据,只修改users_address数据表中的is_delete字段的值为1即可   
            $is = $item->save(['is_delete'=>1]);
            if($is){
                return $this->success('删除成功！','index/user.AddressControl/index');
            }else{
                return $this->error('删除失败！');
            }
        }

    }

}